﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxTabControl;

public partial class PropertyInfo : System.Web.UI.MasterPage
{
    private int _propertyID;

    /*****GIS:start*****/
    private string _parcelID = "";
    private string _address = "";
    private string _property_lat = "";
    private string _property_lng = "";
    private bool _has_trends_team_distance = false;
    /*****GIS:end*****/

    protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            _propertyID = int.Parse(Request.QueryString["pid"]);
        }

        catch
        {
            _propertyID = 0;
        }

        foreach (Tab t in propertyDetailTabs.Tabs)
        {
            t.NavigateUrl += "?pid=" + _propertyID.ToString();
        }

        /*****GIS:start*****/
        if (Request.QueryString["address"] != null)
        {
            _address = Request.QueryString["address"];
        }
        else
        {
            _address = new Gis().getAddress(_propertyID);
        }

        if (Request.QueryString["parcelID"] != null)
        {
            _parcelID = Request.QueryString["_parcelID"];
        }
        else
        {
            _parcelID = new Gis().getParcelID(_propertyID);
        }
        if (Request.QueryString["property_lat"] != null && Request.QueryString["property_lng"] != null)
        {
            _has_trends_team_distance = true;
            _property_lat = Request.QueryString["property_lat"];
            _property_lng = Request.QueryString["property_lng"];
        }
        else
        {
            _has_trends_team_distance = false;
        }

        foreach (Tab t in propertyDetailTabs.Tabs)
        {
            t.NavigateUrl += "&parcel_id=" + _parcelID + "&address=" + _address;
            if (Request.QueryString["property_lat"] != null && Request.QueryString["property_lng"] != null)
            {
                t.NavigateUrl += "&property_lat=" + _property_lat + "&property_lng=" + _property_lng;
            }
        }
        /*****GIS:end*****/
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Property_Data pd = new Property_Data();
        DataSet dsSearchResults = pd.GetDetails(_propertyID);
        Page.Header.Title = "PropertyIQ -- Property Details -- " + dsSearchResults.Tables[0].Rows[0]["street_address"].ToString();
        imgPropertyHeader.ImageUrl = "~/images/house_pic.jpg";
        ltAddress.Text = dsSearchResults.Tables[0].Rows[0]["street_address"].ToString();

        /*****GIS:start*****/
        if (_has_trends_team_distance == true)
        {
            GISInitCommentFrom.Text = "<%--";
            GISInitCommentTo.Text = "--%>";
        }
        else
        {
            if (new Gis().hasTrendsTeamDistance(_propertyID) == true)
            {
                _has_trends_team_distance = true;
            }
        }
        AddressHD.Value = _address;
        HasTreandsTeamDistanceHD.Value = "" + _has_trends_team_distance;
        /*****GIS:end*****/
    }

    public Pages CurrentPage
    {
        set
        {
            propertyDetailTabs.ActiveTabIndex = Convert.ToInt32(value);
        }
    }

    /*****GIS:start*****/
    protected void GIS_Init(object sender, EventArgs e)
    {

        _property_lat = PropertyLatHD.Value;
        _property_lng = PropertyLngHD.Value;

        foreach (Tab t in propertyDetailTabs.Tabs)
        {
            t.NavigateUrl += "&property_lat=" + _property_lat + "&property_lng=" + _property_lng;
        }

        try
        {
            double lat = 0;
            double lng = 0;
            lat = double.Parse(_property_lat);
            lng = double.Parse(_property_lng);

            new Gis().updateLatLng(_propertyID, lat, lng);

            if (_has_trends_team_distance != true)
            {
                string[] table_columns = { "dist_to_park", "dist_to_school", "dist_to_library", "dist_to_mall", "dist_to_hospital" };
                double[] distance = { 0, 0, 0, 0, 0 };

                distance[0] = double.Parse(ParkDistanceHD.Value);
                distance[1] = double.Parse(SchoolDistanceHD.Value);
                distance[2] = double.Parse(LibraryDistanceHD.Value);
                distance[3] = double.Parse(MallDistanceHD.Value);
                distance[4] = double.Parse(HospitalDistanceHD.Value);

                new Gis().updateTrendsTeamDistance(_propertyID, table_columns, distance);
                _has_trends_team_distance = true;
            }
        }
        catch
        {
        }
    }
    /*****GIS:end*****/

    public int PropertyID
    {
        get
        {
            return _propertyID;
        }
    }

    /*****GIS:start*****/
    public string Address
    {
        get
        {
            return _address;
        }
    }
    public string ParcelID
    {
        get
        {
            return _parcelID;
        }
    }
    public string PropertyLat
    {
        get
        {
            return _property_lat;
        }
    }
    public string PropertyLng
    {
        get
        {
            return _property_lng;
        }
    }
    /*****GIS:end*****/
}

public enum Pages
{
    Details = 0, NearbyForeclosures, CurrentListings, Comps, Media, Discussion
}
